using OpenBlock.Item;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace OpenBlock.Entity
{
    public class ItemStack
    {
        public ItemState Item { get; set; }
        public int Count { get; private set; }

        public ItemStack()
        {
            Item = ItemState.AIR;
            Count = 0;
        }

        public ItemStack(ItemState item, int count = 1)
        {
            Item = item;
            Count = count;
        }

        public void Clear() {
            Item = ItemState.AIR;
            Count = 0;
        }

        public Sprite GetIcon()
        {
            if (Count == 0) return null;
            return ItemRegistry.items[Item.id].GetIcon(Item);
        }

        public bool TryStack(ItemStack stack)
        {
            if (stack.Item == Item || Count == 0)
            {
                int max = ItemRegistry.items[Item.id].MaxStackSize;
                Count = Mathf.Clamp(Count + stack.Count, 0, max);
                stack.Count = Mathf.Clamp(Count + stack.Count - max, 0, max);
                return true;
            }
            return false;
        }
    }
}
